#pragma once

#define _DEVELOPER_MODE_



#include <math.h> 
#include <vector>
#include <string>
#include <algorithm>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

#define LINE          1024
#define IMG_MAX_SIZE  1960
#define MAX_M_BOX_NUM 5000
#define MAX_S_PIX_NUM 5000
#define VGA_WIDTH     640
#define QVGA_WIDTH    320
#define QQVGA_WIDTH   160
#define INIT_MAX_SHORT 65535

#define S_PIX_MIN_SIZE 20

#define _HW_PRC_
#define _SHOW_IMG_
#define _YCBCR_
#define _MSPIX_
#define _AF_Map_
//#define _MCUT_

//#define _FRAMEWORK_A_
#define _FRAMEWORK_B_
//#define _FRAMEWORK_AB_

#ifdef  _FRAMEWORK_A_
#define _MERGE_
#define _BACK_JUDGE_
#define _REFINE_BOX_BY_ROUGH_MAP_
//#define _LOW_CON_JUDGE_
#define COLOR_MODIFY_RATE (0.2f)  //0.5f
#define AFMAP_MODIFY_RATE (0.0f)  //0.0f
#define INIT_SALIENT_NUM 1
#define S_PIX_NUM    255   //255
#define S_PIX_TH     500   //600  
#ifdef _MERGE_
#define S_PIX_MIN_RATE (0.7f)  //0.2
#define S_PIX_MAX_RATE (5000.0f)
#else
#define S_PIX_MIN_RATE (0.3f)
#define S_PIX_MAX_RATE (1.25f)
#endif

#define BAYES_THRES  10  //100
#define HIST_BIN_NUM 4
#define IMG_SIZE     QQVGA_WIDTH
#endif

#ifdef  _FRAMEWORK_B_
//#define _AUTO_BOX_
//#define _OLD_FWB_
#define _BACK_JUDGE_
#define _MERGE_
#define COLOR_MODIFY_RATE (0.2f)  //0.2f
#define AFMAP_MODIFY_RATE (0.0f)  //0.0f
#define INIT_SALIENT_NUM 1
#define S_PIX_NUM    255   //300
#define S_PIX_TH     500   //300
#ifdef _MERGE_
#define S_PIX_MIN_RATE (0.1f) //0.2f
#define S_PIX_MAX_RATE (5000.0f)
#else
#define S_PIX_MIN_RATE (0.3f)
#define S_PIX_MAX_RATE (1.25f)
#endif

#define BAYES_THRES  10  //100
#define HIST_BIN_NUM 4
#define IMG_SIZE     QQVGA_WIDTH
#endif

#ifdef  _FRAMEWORK_AB_
#define _MERGE_
#define _BACK_JUDGE_
#define _REFINE_BOX_BY_ROUGH_MAP_
#define COLOR_MODIFY_RATE (0.2f)  //0.2f
#define INIT_SALIENT_NUM 1
#define S_PIX_NUM    300   //400
#define S_PIX_TH     350   //350
#ifdef _MERGE_
#define S_PIX_MIN_RATE (0.5f)  //0.5
#define S_PIX_MAX_RATE (5000.0f)
#else
#define S_PIX_MIN_RATE (0.3f)
#define S_PIX_MAX_RATE (1.25f)
#endif
#define BAYES_THRES 100
#define HIST_BIN_NUM 32
#define IMG_SIZE     QQVGA_WIDTH
#endif


extern "C" {
#ifdef _HW_PRC_
#include "../prc/prc.h"
#endif
}

typedef struct COLOR24BIT
{
	int c0;
	int c1;
	int c2;
	int c3;
}COLOR24BIT;

typedef struct S_Pix
{
	int size;
	COLOR24BIT color;
	int X;
	int Y;
	short win[4];
	int win_color[3];
	bool is_fg;
	bool is_bg;
	float cs_conf;
	float inter_conf;
	float dis_conf;
	float color_conf;
	float size_conf;
	float salient_conf;
	int   vote_count;
	float conf;
	int   merge_id;
	bool is_overlap;
	int rank;
}S_Pix;

typedef struct SEG_YUV444
{
	unsigned char* p_data;
	int img_width;
	int img_height;
	short x0;
	short x1;
	short y0;
	short y1;
	short touch_x;
	short touch_y;
}SEG_YUV444;

//Evaluation API
typedef struct sp_output
{
	S_Pix*         p_spix;
	string         p_name;
	short*         p_s_pix_map;
	unsigned char* p_src_img;
	unsigned char* p_edge_map;
	unsigned char* p_seg_map;
	unsigned char* p_af_map;
	int            avg_superpix_count;
	bool           boundary_ok;
	int            valid_s_pix_num;

	int layer_id;
	char  is_low_contrast;
	unsigned char low_con_thres;
	short final_win[4];

}sp_output;

extern void access_by_24bit(int img_width, unsigned char* p_img, unsigned char* p_pixel, int x, int y);
extern void set_by_24bit(int img_width, unsigned char* p_img, unsigned char* p_pixel, int x, int y);
extern void access_by_8bit(int img_width, unsigned char* p_img, unsigned char* p_pixel, int x, int y);
extern void set_by_8bit(int img_width, unsigned char* p_img, unsigned char* p_pixel, int x, int y);
extern void access_pixel(int img_width, unsigned char* p_img, int bitcount, unsigned char* p_pixel, int x, int y);
extern void set_pixel(int img_width, unsigned char* p_img, int bitcount, unsigned char* p_pixel, int x, int y);
extern void map_spix_avg_color_to_src_img(S_Pix* p_spix, short* p_label_map, unsigned char* p_src_img, int width, int height);
//extern void draw_contour_around_segments(unsigned char* p_edge_map, unsigned char* p_dst_img, short* p_label_map, int img_width, int img_height, bool boundary_ok);
extern void draw_contour_around_segments(sp_output* p_s_evaluate, unsigned char* p_dst_img, int img_width, int img_height);
extern void draw_spix_center_point(S_Pix* p_spix, int valid_s_pix_num, unsigned char* p_dst_img, int img_width, int img_height);
extern void draw_segmentation_result(sp_output* p_s_evaluate, int valid_s_pix_num, unsigned char* p_result_img, int img_width, int img_height, 
							bool draw_contour, bool fill_avg_color, bool draw_center, bool draw_win, bool zero_back,  bool zero_fore, bool fill_saliency);
extern void fill_color_for_spix_by_anotation_map(short* p_s_pix_map, unsigned char* p_anotation_map, int width, int height, S_Pix* p_spix, int valid_s_pix_num);
extern void fill_color_for_spix_by_src_img(short* p_s_pix_map, unsigned char* p_src_map, int width, int height, S_Pix* p_spix, int valid_s_pix_num);
extern float log2(float n);

extern void bitmap_to_yuv444(unsigned char* p_bitmap, int bitmap_width, int bitmap_height, int bitmap_linebyte, SEG_YUV444* p_yuv444);
extern void clone_yuv444(SEG_YUV444* p_src, SEG_YUV444* p_dst, int touch_x, int touch_y, int start_layer, int end_layer);
extern void yuv422_to_yuv444(unsigned char* p_yuv422, unsigned char* p_yuv444, int img_width, int img_height, int seg_422_bytewidth);
extern unsigned char* read_bmp(char* p_file_dir, int* p_img_width, int* p_img_height);

extern unsigned char g_p_yuv444_l0[];
extern unsigned char g_p_yuv444_l1[];
extern unsigned char g_p_yuv444_l2[];
extern unsigned char g_p_yuv444_copy_l0[];
extern unsigned char g_p_yuv444_copy_l1[];
extern unsigned char g_p_yuv444_copy_l2[];